package user

import (
	"day12_11_homework/config"
	"day12_11_homework/internal/models/dao"
	"day12_11_homework/internal/models/response"
	"day12_11_homework/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

type userPostRegister struct {
	Tel      string `json:"tel" binding:"required"`
	Password string `json:"password" binding:"required"`
	Code     string `json:"code"`
}

func UserRegister(c *gin.Context) {

	var userPost userPostRegister

	if err := c.ShouldBindJSON(&userPost); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	user := dao.User{
		Tel:      userPost.Tel,
		Password: pkg.Md5(userPost.Password + "1234"),
		Status:   1,
	}

	response.GetSendSms(userPost.Tel, userPost.Code)

	registerResponse := response.UserRegisterResponse(&user, userPost.Tel, userPost.Code)

	if registerResponse != "" {
		c.JSON(500, gin.H{
			"message": registerResponse,
		})
		return
	}

	Num := "sendSms_register" + userPost.Tel

	c.JSON(200, gin.H{
		"message": "注册成功",
		"data":    user.ID,
	})

	config.Rdb.Del(config.Ctx, Num)

}
